#include "head.h"
/**
 * 参数：所有用户的信息
 * 返回值：登录成功后返回用户信息
 */
U_data Log_in(U_list *list)
{
    if (list == NULL)
    {
        printf("S_List_log_in : list = NULL\n");
        exit(-1);
    }
    if (list->next == NULL)
    {
        printf("S_List_log_in : list->next = NULL\n");
        exit(-1);
    }

    U_data data;
    int num = 0;
    // 开始登录
    while (1)
    {
        if (num >= 3)
        {
            printf("连续三次输入错误，请重新启动程序。\n");
            exit(-1);
        }
        system("clear");
        printf("请输入账号: ");
        scanf("%s", data.name);

        printf("请输入密码: ");
        scanf("%s", data.passwd);

        U_node *ptr = list->next;

        while (ptr != NULL)
        {
            // 比较用户名
            if (strcmp(data.name, ptr->data.name) == 0)
            {
                // 比较密码
                if (strcmp(data.passwd, ptr->data.passwd) == 0)
                {
                    return ptr->data; // 登录成功，返回用户信息
                }
            }
            ptr = ptr->next;
        }
        num++;
    }
}

/**
 * 功能：用户注册
 * 参数：所有用户的信息
 * 返回值：注册成功后返回用户信息
 */
U_data Register(U_list *list, FlightSystem Flight_Sys)
{
    if (list == NULL)
    {
        printf("S_List_log_in : list = NULL\n");
        exit(-1);
    }
    if (list->next == NULL)
    {
        printf("S_List_log_in : list->next = NULL\n");
        exit(-1);
    }

    U_data data; // 开始注册
    printf("请输入账号: ");
    scanf("%s", data.name);

    printf("请输入密码: ");
    scanf("%s", data.passwd);
    data.value = 0;
    data.grade = 2; // 普通用户
    U_node *ptr = list->next;

    while (ptr != NULL)
    {
        // 比较用户名
        if (strcmp(data.name, ptr->data.name) == 0)
        {
            printf("该用户名已被注册，请重新输入\n");
            return Register(list, Flight_Sys);
        }
        ptr = ptr->next;
    }
    S_List_insert_tail(Flight_Sys.REGULAR_USER, data); // 注册成功，将用户信息插入链表
    S_List_File_insert(data, REGULAR_FILE);            // 将用户信息插入文件
    return data;                                       // 注册成功，返回用户信息
}
// ========================================================================================普通用户购票
int buy_ticket_REGULAR_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    D_List_print(Flight_Sys.Flight_list);
    U_node *ptr = S_List_find_name(Flight_Sys.REGULAR_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        return 0;
    }
    printf("你是普通用户,你的余额:%d\n", ptr->data.value);
    printf("请输入要购票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    int i = find_by_id(L, flightnum); // 判断是否已经购买过该航班
    if (i != EOF)
    {
        printf("该航班已购买，无法再次购买\n");
        usleep(10 * 100000);
        return 0;
    }
    if (temp->data.whetherfly == 'y' || temp->data.leftbuyersa == 0)
    {
        printf("该航班无座或已起飞,无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    if (ptr->data.value < temp->data.value)
    {
        printf("用户余额不足，无法购票\n");
        return 0;
    }
    printf("该航班票价为：%d 确认购买吗(回车确认)\n", temp->data.value);
    getchar();
    if (getchar() != '\n')
    {
        printf("取消购买,正在返回\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value -= temp->data.value;
    temp->data.leftbuyersa--;
    tail_insert_tail(L, temp->data);
    S_List_File_print_(Flight_Sys.REGULAR_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
// ==================================================================================================普通用户退票
int refund_ticket_REGULAR_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    int i = my_order(Flight_Sys.Flight_list, log_data, L);
    if(i == 0)
    {
        usleep(10 * 100000);
        return 0;
    }
    U_node *ptr = S_List_find_name(Flight_Sys.REGULAR_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("请输入要退票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value += temp->data.value;
    temp->data.leftbuyersa++;
    id_del_list(L, temp->data);
    S_List_File_print_(Flight_Sys.REGULAR_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
// ======================================================================普通用户余额充值
void recharge_REGULAR_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.REGULAR_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入要充值的金额：");
    int money;
    scanf("%d", &money);
    ptr->data.value += money;
    S_List_File_print_(Flight_Sys.REGULAR_USER, Flight_Sys, log_data.name);
}
// ======================================================================普通用户修改密码
void change_password_REGULAR_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.REGULAR_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入新密码："); // 修改密码
    scanf("%s", ptr->data.passwd);
    S_List_File_print_(Flight_Sys.REGULAR_USER, Flight_Sys, log_data.name);
}
// ======================================================================白银用户购票
int buy_ticket_SILVER_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    D_List_print(Flight_Sys.Flight_list);
    U_node *ptr = S_List_find_name(Flight_Sys.SILVER_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("你是白银用户,购票九折,你的余额:%d\n", ptr->data.value);
    printf("请输入要购票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    int i = find_by_id(L, flightnum); // 判断是否已经购买过该航班
    if (i != EOF)
    {
        printf("该航班已购买，无法再次购买\n");
        usleep(10 * 100000);
        return 0;
    }
    if (temp->data.leftbuyersa == 0 || temp->data.whetherfly == 'y')
    {
        printf("该航班无座或已起飞，无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    if (ptr->data.value < temp->data.value)
    {
        printf("用户余额不足，无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("该航班票价为：%d 确认购买吗(回车确认)\n", temp->data.value);
    getchar();
    if (getchar() != '\n')
    {
        printf("取消购买,正在返回\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value -= (temp->data.value * SILVER_DISCOUNT);
    temp->data.leftbuyersa--;
    tail_insert_tail(L, temp->data);
    S_List_File_print_(Flight_Sys.SILVER_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
//====================================================================================银卡会员退票
int refund_ticket_SILVER_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    int i = my_order(Flight_Sys.Flight_list, log_data, L);
    if(i == 0)
    {
        usleep(10 * 100000);
        return 0;
    }
    U_node *ptr = S_List_find_name(Flight_Sys.SILVER_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("请输入要退票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value += (temp->data.value * SILVER_DISCOUNT);
    temp->data.leftbuyersa++;
    id_del_list(L, temp->data);
    S_List_File_print_(Flight_Sys.SILVER_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
// ======================================================================白银用户余额充值
void recharge_SILVER_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.SILVER_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入要充值的金额：");
    int money;
    scanf("%d", &money);
    ptr->data.value += money;
    S_List_File_print_(Flight_Sys.SILVER_USER, Flight_Sys, log_data.name);
}
// ======================================================================白银用户修改密码
void change_password_SILVER_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.SILVER_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入新密码："); // 修改密码
    scanf("%s", ptr->data.passwd);
    S_List_File_print_(Flight_Sys.SILVER_USER, Flight_Sys, log_data.name);
}
// ==============================================================================================黄金用户购票

int buy_ticket_GOLD_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    D_List_print(Flight_Sys.Flight_list);
    U_node *ptr = S_List_find_name(Flight_Sys.GOLD_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("你是黄金用户,购票八折,你的余额:%d\n", ptr->data.value);
    printf("请输入要购票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    int i = find_by_id(L, flightnum); // 判断是否已经购买过该航班
    if (i != EOF)
    {
        printf("该航班已购买，无法再次购买\n");
        usleep(10 * 100000);
        return 0;
    }
    if (temp->data.leftbuyersa == 0 || temp->data.whetherfly == 'y')
    {
        printf("该航班无座或已起飞,无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    if (ptr->data.value < temp->data.value)
    {
        printf("用户余额不足，无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("该航班票价为：%d 确认购买吗(回车确认)\n", temp->data.value);
    getchar();
    if (getchar() != '\n')
    {
        printf("取消购买,正在返回\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value -= (temp->data.value * GOLD_DISCOUNT);
    temp->data.leftbuyersa--;
    tail_insert_tail(L, temp->data);
    S_List_File_print_(Flight_Sys.GOLD_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
// ==================================================================================================黄金会员退票
int refund_ticket_GOLD_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    int i = my_order(Flight_Sys.Flight_list, log_data, L);
    if(i == 0)
    {
        usleep(10 * 100000);
        return 0;
    }
    U_node *ptr = S_List_find_name(Flight_Sys.GOLD_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("请输入要退票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value += (temp->data.value * GOLD_DISCOUNT);
    temp->data.leftbuyersa++;
    id_del_list(L, temp->data);
    S_List_File_print_(Flight_Sys.GOLD_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
// ======================================================================黄金用户余额充值
void recharge_GOLD_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.GOLD_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入要充值的金额：");
    int money;
    scanf("%d", &money);
    ptr->data.value += money;
    S_List_File_print_(Flight_Sys.GOLD_USER, Flight_Sys, log_data.name);
}
// ======================================================================黄金用户修改密码
void change_password_GOLD_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.GOLD_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入新密码："); // 修改密码
    scanf("%s", ptr->data.passwd);
    S_List_File_print_(Flight_Sys.GOLD_USER, Flight_Sys, log_data.name);
}
// ==============================================================================================白金会员购票
// 白金会员购票
int buy_ticket_PLATINUM_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    D_List_print(Flight_Sys.Flight_list);
    U_node *ptr = S_List_find_name(Flight_Sys.PLATINUM_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("你是白金用户,购票七折,你的余额:%d\n", ptr->data.value);
    printf("请输入要购票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    int i = find_by_id(L, flightnum); // 判断是否已经购买过该航班
    if (i != EOF)
    {
        printf("该航班已购买，无法再次购买\n");
        usleep(10 * 100000);
        return 0;
    }
    if (temp->data.leftbuyersa == 0 || temp->data.whetherfly == 'y')
    {
        printf("该航班无座或已起飞,无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    if (ptr->data.value < temp->data.value)
    {
        printf("用户余额不足，无法购票\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("该航班票价为：%d 确认购买吗(回车确认)\n", temp->data.value);
    getchar();
    if (getchar() != '\n')
    {
        printf("取消购买,正在返回\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value -= (temp->data.value * PLATINUM_DISCOUNT);
    temp->data.leftbuyersa--;
    tail_insert_tail(L, temp->data);
    S_List_File_print_(Flight_Sys.PLATINUM_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
//=======================================================================================白金会员退票
int refund_ticket_PLATINUM_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    int i = my_order(Flight_Sys.Flight_list, log_data, L);
    if(i == 0)
    {
        usleep(10 * 100000);
        return 0;
    }
    U_node *ptr = S_List_find_name(Flight_Sys.PLATINUM_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    printf("请输入要退票的航班号：");
    int flightnum;
    scanf("%d", &flightnum);
    F_node *temp = D_List_find_id(Flight_Sys.Flight_list, flightnum);
    if (temp == NULL)
    {
        printf("该航班不存在，请重新输入\n");
        usleep(10 * 100000);
        return 0;
    }
    ptr->data.value += (temp->data.value * PLATINUM_DISCOUNT);
    temp->data.leftbuyersa++;
    id_del_list(L, temp->data);
    S_List_File_print_(Flight_Sys.PLATINUM_USER, Flight_Sys, log_data.name);
    D_List_file_print(Flight_Sys.Flight_list);
    return flightnum;
}
// ======================================================================白金用户余额充值
void recharge_PLATINUM_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.PLATINUM_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入要充值的金额：");
    int money;
    scanf("%d", &money);
    ptr->data.value += money;
    S_List_File_print_(Flight_Sys.PLATINUM_USER, Flight_Sys, log_data.name);
}

// ======================================================================白金用户修改密码
void change_password_PLATINUM_USER(FlightSystem Flight_Sys, U_data log_data)
{
    U_node *ptr = S_List_find_name(Flight_Sys.PLATINUM_USER, log_data.name);
    if (ptr == NULL)
    {
        printf("该用户不存在，请重新输入\n");
        usleep(10 * 100000);
        return;
    }
    printf("请输入新密码："); // 修改密码
    scanf("%s", ptr->data.passwd);
    S_List_File_print_(Flight_Sys.PLATINUM_USER, Flight_Sys, log_data.name);
}

//==============================================================================不同用户购票
int buy_ticket_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    if (log_data.grade == 2)
    {
        return buy_ticket_REGULAR_USER(Flight_Sys, log_data, L);
    }
    if (log_data.grade == 3)
    {
        return buy_ticket_SILVER_USER(Flight_Sys, log_data, L);
    }
    if (log_data.grade == 4)
    {
        return buy_ticket_GOLD_USER(Flight_Sys, log_data, L);
    }
    if (log_data.grade == 5)
    {
        return buy_ticket_PLATINUM_USER(Flight_Sys, log_data, L);
    }
    return 0;
}

//==============================================================================不同用户退票
int refund_ticket_USER(FlightSystem Flight_Sys, U_data log_data, S_arr_list *L)
{
    if (log_data.grade == 2)
    {
        return refund_ticket_REGULAR_USER(Flight_Sys, log_data, L);
    }
    if (log_data.grade == 3)
    {
        return refund_ticket_SILVER_USER(Flight_Sys, log_data, L);
    }
    if (log_data.grade == 4)
    {
        return refund_ticket_GOLD_USER(Flight_Sys, log_data, L);
    }
    if (log_data.grade == 5)
    {
        return refund_ticket_PLATINUM_USER(Flight_Sys, log_data, L);
    }
    return 0;
}

//==============================================================================不同用户充值
// 根据用户等级进行充值
void recharge_USER(FlightSystem Flight_Sys, U_data log_data)
{
    // 如果用户等级为2，则调用普通用户充值函数
    if (log_data.grade == 2)
    {
        recharge_REGULAR_USER(Flight_Sys, log_data);
    }
    // 如果用户等级为3，则调用银牌用户充值函数
    if (log_data.grade == 3)
    {
        recharge_SILVER_USER(Flight_Sys, log_data);
    }
    // 如果用户等级为4，则调用金牌用户充值函数
    if (log_data.grade == 4)
    {
        recharge_GOLD_USER(Flight_Sys, log_data);
    }
    // 如果用户等级为5，则调用白金用户充值函数
    if (log_data.grade == 5)
    {
        recharge_PLATINUM_USER(Flight_Sys, log_data);
    }
    // 返回
    return;
}
//==============================================================================不同用户修改密码
void change_password_USER(FlightSystem Flight_Sys, U_data log_data)
{
    if (log_data.grade == 2)
    {
        change_password_REGULAR_USER(Flight_Sys, log_data);
    }
    if (log_data.grade == 3)
    {
        change_password_SILVER_USER(Flight_Sys, log_data);
    }
    if (log_data.grade == 4)
    {
        change_password_GOLD_USER(Flight_Sys, log_data);
    }
    if (log_data.grade == 5)
    {
        change_password_PLATINUM_USER(Flight_Sys, log_data);
    }
    return;
}
// ======================================================================查询订单
int my_order(F_list *Flight_list, U_data log_data, S_arr_list *L)
{
    int i = printf_S_arr_list(L);
    if(i == 0)
    {
        return 0;
    }
    return 1;
}